package com.af.community.smart.business

import android.os.Bundle
import android.view.View
import android.widget.ImageView
import androidx.recyclerview.widget.GridLayoutManager
import com.af.community.smart.base.BaseActivity
import com.af.community.smart.base.addItems
import com.af.community.smart.base.loadGrid
import com.af.community.smart.base.loadImage
import com.af.community.smart.model.GuideData
import com.af.community.smart.share.Const
import com.af.community.smart.utils.MultiGapDecoration
import com.af.community.smart.utils.Tools
import com.af.community.smart.utils.bindLifeCycle
import com.af.community.smart.utils.subscribeByFinally
import com.af.community.smart.viewmodel.HandleViewModel
import com.baidu.mapapi.isAvilible
import kotlinx.android.synthetic.main.layout_list.*
import net.idik.lib.slimadapter.SlimAdapter
import org.jetbrains.anko.include
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.toast
import org.koin.androidx.viewmodel.ext.android.getViewModel

class HandleActivity : BaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        include<View>(R.layout.layout_list)
        initTitle(title = "办事指南查询")

        getViewModel().getGuideList()
            .bindLifeCycle(getLifecycleOwner())
            .subscribeByFinally(
                event = getViewModel(),
                onSuccess = { item ->
                    item.forEach {
                        mList.add(it.name)
                        mList.addItems(it.typeList)
                    }
                    mAdapter.updateData(mList)
                }
            )
    }

    override fun getViewModel() = getViewModel<HandleViewModel>()

    override fun initTitle(title: String, name: String) {
        super.initTitle(title, name)
        swipe_refresh.isEnabled = false
        recycle_list.loadGrid {
            addItemDecoration(MultiGapDecoration(5))
            layoutManager = GridLayoutManager(baseContext, 3).apply {
                spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
                    override fun getSpanSize(position: Int) = when {
                        mAdapter.getItem(position) is GuideData -> 1
                        else -> 3
                    }
                }
            }
        }

        mAdapter = SlimAdapter.create()
            .register<String>(R.layout.item_handle_divider) { data, injector ->
                injector.text(R.id.item_handle_title, data)
            }
            .register<GuideData>(R.layout.item_handle_list) { data, injector ->
                injector.text(R.id.item_handle_name, data.name)
                    .with<ImageView>(R.id.item_handle_img) {
                        it.loadImage(data.attr1 ?: "", R.mipmap.default_logo3)
                    }
                    .clicked(R.id.item_handle) {
                        when (data.code) {
                            "0", "4" ->
                                startActivity<HandleListActivity>(
                                    "title" to data.name,
                                    "id" to data.id,
                                    "type" to data.code
                                )
                            "1" -> {
                                getDetail(data.id, data.name, "text")
                                // startActivity<HandleFundActivity>()
                            }
                            "2" -> {
                                getDetail(data.id, data.name, "url")
                                // startActivity<HandleApplyActivity>()
                            }
                            "3" -> {
                                val isAMapInstalled = isAvilible(Const.aMapPkg)
                                val isBMapInstalled = isAvilible(Const.bMapPkg)
                                val isTMapInstalled = isAvilible(Const.tMapPkg)
                                val isGMapInstalled = isAvilible(Const.gMapPkg)

                                when {
                                    isBMapInstalled -> Tools.openAppWithPackageName(
                                        baseContext,
                                        Const.bMapPkg
                                    )
                                    isAMapInstalled -> Tools.openAppWithPackageName(
                                        baseContext,
                                        Const.aMapPkg
                                    )
                                    isTMapInstalled -> Tools.openAppWithPackageName(
                                        baseContext,
                                        Const.tMapPkg
                                    )
                                    isGMapInstalled -> Tools.openAppWithPackageName(
                                        baseContext,
                                        Const.gMapPkg
                                    )
                                    else -> toast("请先安装地图软件！")
                                }
                            }
                            else -> startActivity<HandleTaxActivity>()
                        }
                    }
            }
            .attachTo(recycle_list)
    }

    private fun getDetail(typeId: String, title: String, type: String) {
        getViewModel().getGuideDetail(typeId)
            .bindLifeCycle(getLifecycleOwner())
            .subscribeByFinally(
                event = getViewModel(),
                onSuccess = {
                    if (!it.isNullOrEmpty()) {
                        startActivity<WebActivity>(
                            "type" to type,
                            "title" to title,
                            "content" to (it[0].content)
                        )
                    }
                }
            )
    }

}
